package web.suzy.oj.service.admin.problem.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import web.suzy.oj.common.exception.StatusFailException;
import web.suzy.oj.common.exception.StatusForbiddenException;
import web.suzy.oj.common.result.ResultBean;
import web.suzy.oj.common.result.ResultStatus;
import web.suzy.oj.manager.admin.problem.AdminProblemManager;
import web.suzy.oj.pojo.dto.ProblemDTO;
import web.suzy.oj.pojo.entity.problem.Problem;
import web.suzy.oj.pojo.entity.problem.ProblemCase;
import web.suzy.oj.service.admin.problem.AdminProblemService;

import java.util.List;

/**
 * YangSuzy 软件工作室
 * 类名: AdminProblemServiceImpl
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/22 14:46
 */
@Service
public class AdminProblemServiceImpl implements AdminProblemService {

    @Autowired
    private AdminProblemManager adminProblemManager;

    /**
     * 方法名: getProblemList
     * 描述: 获取题目列表
     *
     * @param limit       每页显示数目
     * @param currentPage 当前页
     * @param keyword     关键字
     * @param auth        题目权限 1-公开 2-私有
     * @return web.suzy.oj.common.result.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < web.suzy.oj.pojo.entity.problem.Problem>>
     * @date 2022/11/22 14:47
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<IPage<Problem>> getProblemList(Integer limit, Integer currentPage, String keyword, Integer auth) {
        IPage<Problem> problemList = adminProblemManager.getProblemList(limit, currentPage, keyword, auth);
        return ResultBean.successResponse(problemList);
    }

    /**
     * 方法名: getProblem
     * 描述: 查看题目信息
     *
     * @param pid 题目ID
     * @return web.suzy.oj.common.result.ResultBean<web.suzy.oj.pojo.entity.problem.Problem>
     * @date 2022/11/22 15:05
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Problem> getProblem(Long pid) {
        try {
            Problem problem = adminProblemManager.getProblem(pid);
            return ResultBean.successResponse(problem);
        } catch (StatusForbiddenException e) {
            return ResultBean.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        }
    }

    /**
     * 方法名: deleteProblem
     * 描述: 删除题目
     *
     * @param pid 题目ID
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 15:35
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Void> deleteProblem(Long pid) {
        try {
            adminProblemManager.deleteProblem(pid);
            return ResultBean.successResponse();
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        }
    }

    /**
     * 方法名: addProblem
     * 描述: 添加题目
     *
     * @param problemDto 题目信息传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 15:50
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Void> addProblem(ProblemDTO problemDto) {
        try {
            adminProblemManager.addProblem(problemDto);
            return ResultBean.successResponse();
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        }
    }

    /**
     * 方法名: updateProblem
     * 描述: 更新题目信息
     *
     * @param problemDto 题目信息传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 16:07
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Void> updateProblem(ProblemDTO problemDto) {
        try {
            adminProblemManager.updateProblem(problemDto);
            return ResultBean.successResponse();
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        } catch (StatusForbiddenException e) {
            return ResultBean.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }

    /**
     * 方法名: getProblemCases
     * 描述: 获取题目测试用例
     *
     * @param pid      题目ID
     * @param isUpload 是否使用上传的文件
     * @return web.suzy.oj.common.result.ResultBean<java.util.List < web.suzy.oj.pojo.entity.problem.ProblemCase>>
     * @date 2022/11/22 18:29
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<List<ProblemCase>> getProblemCases(Long pid, Boolean isUpload) {
        List<ProblemCase> problemCaseList = adminProblemManager.getProblemCases(pid, isUpload);
        return ResultBean.successResponse(problemCaseList);
    }

    /**
     * 方法名: changeProblemAuth
     * 描述: 修改题目权限
     *
     * @param problem 题目信息
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 18:40
     * @auther YangSuzy
     **/
    @Override
    public ResultBean<Void> changeProblemAuth(Problem problem) {
        try {
            adminProblemManager.changeProblemAuth(problem);
            return ResultBean.successResponse();
        } catch (StatusFailException e) {
            return ResultBean.errorResponse(e.getMessage());
        } catch (StatusForbiddenException e) {
            return ResultBean.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }
}
